pacman::p_load(dplyr,stargazer,ggplot2)

df <- read.csv("pausellietal.csv")

'%!in%' <- function(x,y)!('%in%'(x,y))

chinaabsent <- df$title[df$year==2013 | df$year==2020 & !is.na(df$favor_num)]
chinapresentonly <- df$title[df$year!=2013 & df$year!=2020 & !is.na(df$favor_num)]
chinapresentonly <- chinapresentonly[chinapresentonly %!in% chinaabsent]
chinapresentonly <- data.frame(title=chinapresentonly)
chinapresentonly <- chinapresentonly %>% group_by(title) %>% summarise(n=n())
chinapresentonly <- chinapresentonly[chinapresentonly$n>50,]

df2 <- df[,c("year","id","title","china_vote")]
df2 <- unique(df2)
df3 <- df2 %>% group_by(title) %>% summarise(n=n())
df2 <- merge(df2,df3,by="title",all.x=TRUE)
df2 <- df2[df2$n>1 & df2$n<22,]
df2$chinapresentonly <- ifelse(df2$title %in% chinapresentonly$title,1,0)
df2 <- df2[!is.na(df2$china_vote),]
df2$type_resolution <- ifelse(df2$chinapresentonly==1,"Only Treatment Years","Treatment and Control Years")
df4 <- df2[,c("title","china_vote","type_resolution")]
df4 <- unique(df4)
dat <- df4 %>% count(china_vote, type_resolution) %>%
  group_by(type_resolution) %>%
  mutate(percent = n / sum(n),
         error = sqrt((percent * (1-percent))/n))

dat$lb <- dat$percent-dat$error
dat$ub <- dat$percent+dat$error
dat$lb <- ifelse(dat$lb<0,0,dat$lb)


png("Figure_6.png",4500,2000,res=650)
ggplot(dat, aes(china_vote, percent, fill = type_resolution)) + 
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = lb, ymax = ub),
                position = position_dodge(0.9)) +
  ylab("Proportion of resolutions") + xlab("China's vote") +
  scale_fill_grey(start = 0.5, end = .9) +
  labs(fill="") + theme_bw()
dev.off()

df5 <- df4[df4$type_resolution=="Treatment and Control Years",]
df5 <- df5[,c("title","china_vote")]

stargazer(df5,summary=FALSE,rownames = FALSE)